package com.qqcommon;

import com.qqserver.service.ClientConnectServerThread;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class UserClientServer {
    private User user=new User();
    private Socket socket;


    public boolean checkUser(String userid,String pwd){
         boolean b=false;
        //创建user对象
        user.setUserid(userid);//这里报错了
        user.setPassword(pwd);
        //连接服务端,获取u对象
        try {
            socket=new Socket(InetAddress.getLocalHost(),9999);//这里的IP!!!error
            //得到ObjectOutputStream对象
            ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(user);//发送userduix

            //读取从服务器回复的message对象
            ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
            Message message=(Message) ois.readObject();

            if(message.getMesType().equals("MESSAGE_LOGIN_SUCCEED")){//登录成功
                //创建一个和服务器端保持通信的线程,
                ClientConnectServerThread clientConnectServerThread = new ClientConnectServerThread(socket);
                //启动客户端线程
                clientConnectServerThread.start();
                //为了后面客户端的扩展,我们将线程放入一个集合中去
                ManageClientServerThread.addClientConnectServerThread(userid,clientConnectServerThread);
                //
                b=true;

            }else{
                //登录失败,关闭socket
                System.out.println("登录失败");
                socket.close();
            }


        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return b;
    }
}
